package com.company.gxcpc2018;

import java.util.Scanner;

public class Gxcpc9 {

    static int[] map = new int[]{6, 2, 5, 5, 4, 5, 6, 3, 7, 6};

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            sc.nextLine();
            String str = sc.nextLine();
            int num = 0;
            for (char aChar : str.toCharArray()) {
                num += map[aChar - '0'];
            }
            StringBuilder led = new StringBuilder();
            int len = num / 7;
            while (len-- > 1) {
                led.append('8');
            }
            if (num % 7 > 1) {
                StringBuilder number = new StringBuilder();
                if (num / 7 != 0) {
                    number.append("8");
                }
                led.insert(0, (num % 7 == 6 ? "6" : getIndex(num % 7)) + number.toString());
            } else if (num % 7 == 1) {
                led.insert(0, "16");
            }
            System.out.println(led);
        }
    }

    public static int getIndex(int num) {
        for (int i = 0; i < map.length; i++) {
            if (map[i] == num) {
                return i;
            }
        }
        return -1;
    }
}
